package com.snowwolf.user.rest.mongodb;

import com.snowwolf.baseUtil.base.RestResponse;
import com.snowwolf.baseUtil.page.mongodb.PageUtil2Mdb;
import com.snowwolf.user.entity.mongodb.NewsMdbEo;
import com.snowwolf.user.service.mongodb.INewService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.mongodb.core.ExecutableUpdateOperation;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static org.springframework.data.mongodb.core.query.Query.query;

/**
 * @author: topsnowwolf
 * @description: 通过mongoTemplate直接调用提供的方法，或者通过MongoRepository
 * @date: Create in 2019/3/16 9:31
 * @modified by:
 * @versions：0.1.0
 */
@RestController
@RequestMapping("/news")
public class NewsController {
    private static Logger logger = LoggerFactory.getLogger(NewsController.class);
    @Autowired
    private LoadBalancerClient loadBalancerClient;
    @Autowired
    private INewService newService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @PostMapping("/save")
    public RestResponse save(@RequestBody NewsMdbEo newsMdbEo){
        RestResponse restResponse = new RestResponse();
        try {
            ServiceInstance serviceInstance = this.loadBalancerClient.choose("userApi");
            logger.info("serviceId is ={}, host is = {}, port is ={}",serviceInstance.getServiceId(),serviceInstance.getHost(),serviceInstance.getPort());
            restResponse.setData(newService.save(newsMdbEo));
        }catch (Exception e){
            e.printStackTrace();
        }
        return restResponse;
    }

    @PostMapping("/update")
    public RestResponse update(@RequestBody NewsMdbEo newsMdbEo){
        RestResponse restResponse = new RestResponse();
        try {
            ServiceInstance serviceInstance = this.loadBalancerClient.choose("userApi");
            logger.info("serviceId is ={}, host is = {}, port is ={}",serviceInstance.getServiceId(),serviceInstance.getHost(),serviceInstance.getPort());
            restResponse.setData(newService.update(newsMdbEo));
        }catch (Exception e){
            e.printStackTrace();
        }
        return restResponse;
    }

    @GetMapping("/find")
    public RestResponse find(String id)throws Exception{
        RestResponse restResponse = new RestResponse();
        try {
            ServiceInstance serviceInstance = this.loadBalancerClient.choose("userApi");
            logger.info("serviceId is ={}, host is = {}, port is ={}",serviceInstance.getServiceId(),serviceInstance.getHost(),serviceInstance.getPort());
            restResponse.setData(newService.findById(id));
        }catch (Exception e){
            e.printStackTrace();
        }
        return restResponse;
    }

    @GetMapping("/page")
    public RestResponse page(NewsMdbEo newsMdbEo,Integer pageSize,Integer pageNum)throws Exception{
        RestResponse restResponse = new RestResponse();
        PageUtil2Mdb page = new PageUtil2Mdb(pageNum,pageSize);
        try {
            ServiceInstance serviceInstance = this.loadBalancerClient.choose("userApi");
            logger.info("serviceId is ={}, host is = {}, port is ={}",serviceInstance.getServiceId(),serviceInstance.getHost(),serviceInstance.getPort());
            restResponse.setData(newService.findByPage(newsMdbEo,page));
        }catch (Exception e){
            e.printStackTrace();
        }
        return restResponse;
    }

    @GetMapping("/all")
    public RestResponse findAll() throws Exception{
        RestResponse restResponse = new RestResponse();
        try {
            ServiceInstance serviceInstance = this.loadBalancerClient.choose("userApi");
            logger.info("serviceId is ={}, host is = {}, port is ={}",serviceInstance.getServiceId(),serviceInstance.getHost(),serviceInstance.getPort());
            List<NewsMdbEo> all = newService.findAll();
            restResponse.setData(all);
        }catch (Exception e){
            e.printStackTrace();
        }
        return restResponse;
    }


    //通过mongoTemplate-----------------------------------------------------------------------------------

    @PostMapping("/temp/save")
    public RestResponse saveByTemp(@RequestBody NewsMdbEo newsMdbEo){
        RestResponse restResponse = new RestResponse();
        try {
            ServiceInstance serviceInstance = this.loadBalancerClient.choose("userApi");
            logger.info("serviceId is ={}, host is = {}, port is ={}",serviceInstance.getServiceId(),serviceInstance.getHost(),serviceInstance.getPort());
            NewsMdbEo insert = mongoTemplate.insert(newsMdbEo);
            restResponse.setData(insert.getId());
        }catch (Exception e){
            e.printStackTrace();
        }
        return restResponse;
    }

    @PostMapping("/temp/update")
    public RestResponse updateByTemp(@RequestBody NewsMdbEo newsMdbEo){
        RestResponse restResponse = new RestResponse();
        try {
            ServiceInstance serviceInstance = this.loadBalancerClient.choose("userApi");
            logger.info("serviceId is ={}, host is = {}, port is ={}",serviceInstance.getServiceId(),serviceInstance.getHost(),serviceInstance.getPort());
//            mongoTemplate.update(newsMdbEo);
//            restResponse.setData(newService.update(newsMdbEo));
        }catch (Exception e){
            e.printStackTrace();
        }
        return restResponse;
    }

    @GetMapping("/temp/find")
    public RestResponse findByTemp(String id)throws Exception{
        RestResponse restResponse = new RestResponse();
        try {
            ServiceInstance serviceInstance = this.loadBalancerClient.choose("userApi");
            logger.info("serviceId is ={}, host is = {}, port is ={}",serviceInstance.getServiceId(),serviceInstance.getHost(),serviceInstance.getPort());
            restResponse.setData(mongoTemplate.findById(id, NewsMdbEo.class));
        }catch (Exception e){
            e.printStackTrace();
        }
        return restResponse;
    }

    @GetMapping("/temp/page")
    public RestResponse pageByTemp(NewsMdbEo newsMdbEo,Integer pageSize,Integer pageNum)throws Exception{
        RestResponse restResponse = new RestResponse();
        PageUtil2Mdb page = new PageUtil2Mdb(pageNum,pageSize);
        try {
            ServiceInstance serviceInstance = this.loadBalancerClient.choose("userApi");
            logger.info("serviceId is ={}, host is = {}, port is ={}",serviceInstance.getServiceId(),serviceInstance.getHost(),serviceInstance.getPort());
            Query query = query(Criteria.where("type").is(newsMdbEo.getType()));
            long total = mongoTemplate.count(query, NewsMdbEo.class);
            List<NewsMdbEo> list = mongoTemplate.find(query.with(page), NewsMdbEo.class);
            Page<NewsMdbEo> pages = new PageImpl(list, page,total);
            restResponse.setData(pages);
        }catch (Exception e){
            e.printStackTrace();
        }
        return restResponse;
    }
}
